<template>
  <div class="login-container">
    <el-card class="login-card" shadow="always">
      <h2 style="text-align: center;">教师端登录</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="employeeId">
          <el-input type="password" v-model="form.employeeId" maxlength="5" show-word-limit placeholder="请输入5位数字密码" />
        </el-form-item>
        <el-form-item label="请再次输入密码" prop="confirmEmployeeId" class="nowrap-label">
          <el-input type="password" v-model="form.confirmEmployeeId" maxlength="5" show-word-limit placeholder="请再次输入5位数字密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onLogin" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>
      <div v-if="msg" style="color:red;text-align:center;margin-top:10px;">{{ msg }}</div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const formRef = ref()
const form = ref({
  name: '',
  employeeId: '',
  confirmEmployeeId: ''
})
const msg = ref('')

const rules = {
  name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  employeeId: [
    { required: true, message: '请输入5位数字密码', trigger: 'blur' },
    { pattern: /^\d{5}$/, message: '密码必须为5位数字', trigger: 'blur' }
  ],
  confirmEmployeeId: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      if (value !== form.value.employeeId) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ]
}

const onLogin = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return
    const { name, employeeId, confirmEmployeeId } = form.value

    try {
      if (name === 'admin') {
        // 管理员登录
        if (employeeId !== confirmEmployeeId) {
          msg.value = '两次输入的密码不一致'
          return
        }
        const res = await fetch('http://localhost:3000/api/admin/login', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ employeeId })
        })
        const data = await res.json()
        if (res.ok) {
          msg.value = ''
          localStorage.setItem('employeeId', employeeId)
          localStorage.setItem('admin_name', data.admin_name)
          router.push('/admin')
        } else {
          msg.value = data.message || '用户名或密码错误'
        }
      } else {
        // 普通用户登录
        const res = await fetch('http://localhost:3000/api/login', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ name, employeeId, confirmEmployeeId })
        })
        const data = await res.json()
        if (res.ok) {
          msg.value = ''
          localStorage.setItem('employeeId', employeeId)
          localStorage.setItem('name', name)
          router.push('/home')
        } else {
          msg.value = data.message
        }
      }
    } catch (e) {
      msg.value = '网络错误'
    }
  })
}
</script>

<style scoped>
.login-container {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
}
.login-card {
  width: 400px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}
.nowrap-label >>> .el-form-item__label {
  white-space: nowrap;
}
</style>
